<?php
namespace appin\cms\model;

use appin\cms\Model;
use think\model\concern\SoftDelete;

class Articles extends Model
{
    use SoftDelete;

    protected $name="cms_articles";
    protected $autoWriteTimestamp = true;
    protected $deleteTime = 'delete_time';

    public function scopeUseKeyword($query, $keyword)
    {
        if ( !empty($keyword) ) {
            $query->where('title', 'LIKE', '%'.$keyword.'%');
        }
    }

    /**
     * 所属栏目
     * @return mixed
     */
    public function columns()
    {
        return $this->belongsTo(Column::class, 'column_id', 'id');
    }

    /**
     * 设置是否推荐
     * @param {Object} $value
     */
    public function setIsRecAttr($value)
    {
        return $value === 'on' ? 1 : 0;
    }

    /**
     * 获取文章列表
     * @param string $field
     * @param $where
     * @param int $page
     * @param int $limit
     * @param string $order
     * @param bool $cache
     * @return mixed
     */
    public static function lists($field = '*', $where, $page = 1, $limit = 10, $order = 'id desc', $cache = false)
    {
        return static::UseWhere($where)
            ->field($field)
            ->page($page, $limit)
            ->order($order)
            ->cache($cache)
            ->select();
    }
}